﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Mirror;
using MrPP.Network;

namespace MrPP.SerriedCabinet
{
    public class BoxModel : NetworkBehaviour {



        [SyncVar(hook = "changeLPage")]
        public int lPage = 0;

        public Action<int> doLPage
        {
            get;
            set;
        }

      
        public void changeLPage(int oldValue, int newValue)
        {
           // lPage = value;
            doLPage?.Invoke(newValue);
        }

        [SyncVar(hook = "changeRPage")]
        public int rPage = 0;

        public Action<int> doRPage
        {
            get;
            set;
        }

        public void changeRPage(int oldValue, int newValue)
        //public void changeRPage(int value)
        {

           // rPage = value;
            doRPage?.Invoke(newValue);
        }

        [SyncVar(hook = "changeLight")]
        public CabinetGroup.ItemData lit;//亮那个一个

          
        public Action<CabinetGroup.ItemData, CabinetGroup.ItemData> onLight
        {
            get;
            set;
        }

        public void changeLight(CabinetGroup.ItemData oldValue, CabinetGroup.ItemData newValue)
        {
           // Debug.LogError(val.text);
            onLight?.Invoke(oldValue, newValue);
            //lit = val;
        }



        [SyncVar(hook="changeBox")]
        private int box_ = -1;//box
        public int box {
            set {
                box_ = value;
            }
            get {
                return box_;
            }
        }

        public Action<int, int> onBox
        {
            get;
            set;
        }

        public void changeBox(int oldValue, int newValue) {
            onBox?.Invoke(oldValue, newValue);
         //   box_ = val;
        }

        [SyncVar(hook = "changeMenu")]
        public int menu = -1;//menu

        public Action<int, int> onMenu
        {
            get;
            set;
        }

        public void changeMenu(int oldValue, int newValue)
        {
            onMenu?.Invoke(oldValue, newValue);
           // menu = val;
        }

        public class DataSyncList : SyncList<CabinetGroup.ItemData> { }


        public DataSyncList datas = new DataSyncList();
        public Action onRefresh
        {
            get;
            set;
        }
        private SyncListCallback dataCallback_ = null;
        void changed(DataSyncList.Operation op, int index, CabinetGroup.ItemData oldItem, CabinetGroup.ItemData newItem)
        {
            if (dataCallback_ == null) {
                dataCallback_ = this.gameObject.AddComponent<SyncListCallback>();


                dataCallback_.start = delegate
                {
                    onRefresh?.Invoke();
                };
                dataCallback_.action = delegate
                {
                    onRefresh?.Invoke();
                };
            }
            dataCallback_.reset();
        }
        public void Awake()
        {
            datas.Callback += changed;
        }

        internal void clearLight()
        {
            CabinetGroup.ItemData data = new CabinetGroup.ItemData();
            data.type = Box.Type.None;
            this.lit = data;
        }
    }
}
